package com.osmap.openstreetmap.ui.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

import com.osmap.openstreetmap.R;

@SuppressWarnings("deprecation")
public class HomePageActivity extends TabActivity implements OnClickListener {

	private final String HOME = "home";

	private final String CHANNEL = "channel";

	private final String TEAM = "team";

	private final String PERSON = "person";

	private final String INDUSTRY_NEWS = "industryNews";

	private ImageView glideBar;
	private int offset;
	private int currentPage;
	public static TabHost tab;
	public static RadioButton radioButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		offset = calculateGlideOffset();
		initView();
		initTabhost();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		findViewById(R.id.main_tab_home).setOnClickListener(this);
		radioButton = (RadioButton) findViewById(R.id.main_tab_channel);
		radioButton.setOnClickListener(this);
		findViewById(R.id.main_tab_person).setOnClickListener(this);
		findViewById(R.id.main_tab_industry_news).setOnClickListener(this);
	}

	public void initTabhost() {
		addTab(HOME, "首页", HomeActivity.class);
		addTab(CHANNEL, "频道", FootmarkActivity.class);
		addTab(PERSON, "个人中心", CollectionActivity.class);
		addTab(INDUSTRY_NEWS, "行业资讯", FootmarkCleanActivity.class);
		tab = getTabHost();
	}

	public void addTab(String tag, String indicator, Class<?> cls) {
		getTabHost().addTab(
				getTabHost().newTabSpec(tag).setIndicator(indicator)
						.setContent(new Intent(this, cls)));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_tab_home:
			getTabHost().setCurrentTabByTag(HOME);
			break;
		case R.id.main_tab_channel:
			getTabHost().setCurrentTabByTag(CHANNEL);
			break;
		case R.id.main_tab_person:
			getTabHost().setCurrentTabByTag(PERSON);
			break;
		case R.id.main_tab_industry_news:
			getTabHost().setCurrentTabByTag(INDUSTRY_NEWS);
			break;
		}
	}

	private void sendRequestForLogin(String username, String password) {
		Map<String, String> params = buildParams(username, password);

	}

	private Map<String, String> buildParams(String username, String password) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("username", username);
		params.put("password", password);
		return params;
	}

	// private AjaxCallback<LoginReturned> loginCallback = new
	// AjaxCallback<LoginReturned>() {
	//
	// @Override
	// public void callback(String url, LoginReturned object, AjaxStatus status)
	// {
	// InternetReturnedHandler<BaseWebBean> prefixHandler = new
	// InternetReturnedHandler<BaseWebBean>(
	// HomePageActivity.this);
	// if (prefixHandler.advanceHandle(object) == false) {
	// return;
	// }
	//
	// UserInfo userInfo = object.getData();
	// DonYuCache.setUserInfo(userInfo);
	// DonYuCache.setToken(object.getToken());
	//
	// }
	// };

	// private void glideToTab(int currentTab) {
	// if (currentPage == currentTab) {
	// return;
	// }
	//
	// TranslateAnimationFactory animationFactory = new
	// TranslateAnimationFactory(currentPage, offset);
	// Animation animation =
	// animationFactory.createTranslateAnimation(currentTab);
	// currentPage = currentTab;
	// if (animation == null) {
	// // Logger.e(this, "animation is null. primaryIndex:" + currentPage +
	// "  targetTab:" + currentTab);
	// // Toaster.showShort(this, "animation is null. primaryIndex:" +
	// currentPage + "  targetTab:" + currentTab);
	// return;
	// }
	//
	// animation.setFillAfter(true);
	// animation.setDuration(150);
	// glideBar.startAnimation(animation);
	// }

	/**
	 * 计算切换页面时，上面黑色条形需要滑动的距离
	 */
	private int calculateGlideOffset() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;
		return screenWidth / 4;
	}

	long endTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - endTime) > 2000) {
				Toast.makeText(this, "再按一次退出！", Toast.LENGTH_SHORT).show();
				endTime = System.currentTimeMillis();
			} else {
				finish();
				System.exit(0);
			}
			// 捕获子activity的返回事件
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
